package gui;

import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JButton;

import client.Client;
import client.Item;
import client.catalog;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTree;

public class UpdateSale extends JPanel{

	private JComboBox jComboBox = null;
	private JPanel jPanel = null;
	private JButton addButton = null;
	private JButton updateButton = null;
	public List<Item> items;  //  @jve:decl-index=0:
	private Client c;
	private JTextArea priceTextArea = null;
	private JLabel price = null;
	private JLabel newPrice = null;
	private JTextField priceTextField = null;
	private JButton addButton1 = null;
	private JPanel okPanel = null;
	private JButton okButton = null;
	private JLabel textLabel = null;
	private JButton jButton = null;
	private JButton backButton = null;
	private JTextArea jTextArea = null;
	private JTextArea newSaleArea = null;
	private JLabel updateLabel = null;
	private JButton delButton = null;
	private JPanel pricePanel = null;
	private JButton delButton1 = null;
	private JPanel okDelPanel = null;
	private JLabel delLabel = null;
	private JButton okDelButton = null;
	private JPanel errorPanel = null;
	private JButton okButton1 = null;
	private JLabel errorLabel = null;
	/**
	 * 
	 */
	public UpdateSale() {
		super();
		getItems();
		initialize();
	}
	
	public void getItems(){
		catalog cat = new catalog();
		Object b = cat.itemDisply();
		items = (List<Item>) b;
	}
	private void initialize() {
		this.setSize(1000, 700);
		this.setLayout(null);
		this.add(getJPanel(), null);
		this.add(getAddButton(), null);
		this.add(getUpdateButton(), null);
		this.add(getBackButton(), null);
		this.add(getDelButton(), null);
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setBounds(new Rectangle(207, 37, 300, 50));
			jComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String name = (String)jComboBox.getSelectedItem();
					int p = 0;
					for (int i=0;i<items.size();i++){
						if (items.get(i).getName().equals(name))
							p = items.get(i).getPrice();
							priceTextArea.setText(Integer.toString(p));
					}
				}
			});
		}
		return jComboBox;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			updateLabel = new JLabel();
			updateLabel.setText("THE UPDATE SALE PRICE IS:");
			updateLabel.setBounds(new Rectangle(2, 95, 175, 26));
			newPrice = new JLabel();
			newPrice.setText("NEW PRICE:");
			newPrice.setBounds(new Rectangle(196, 7, 77, 36));
			price = new JLabel();
			price.setText("PRICE:");
			//jPanel.setBackground(Color.ORANGE);
			price.setBounds(new Rectangle(44, 5, 60, 30));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(166, 215, 710, 268));
			jPanel.add(getJComboBox(), null);
			jPanel.add(getAddButton1(), null);
			jPanel.add(getOkPanel(), null);
			jPanel.add(getJButton(), null);
			jPanel.add(getJTextArea(), null);
			jPanel.add(getPricePanel(), null);
			jPanel.add(getDelButton1(), null);
			jPanel.add(getOkDelPanel(), null);
			jPanel.add(getErrorPanel(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes addButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getAddButton() {
		if (addButton == null) {
			addButton = new JButton();
			addButton.setBounds(new Rectangle(228, 76, 150, 62));
			addButton.setText("ADD TO SALE");
		}
		addButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				addButton1.setVisible(false);
				jButton.setVisible(true);
				pricePanel.setVisible(true);
				for (int i=0;i<items.size();i++){
					if (!(items.get(i).getSaleFlag()))
						jComboBox.addItem(items.get(i).getName());
				}
			}
		});
		return addButton;
	}

	/**
	 * This method initializes updateButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getUpdateButton() {
		if (updateButton == null) {
			updateButton = new JButton();
			updateButton.setBounds(new Rectangle(614, 73, 187, 65));
			updateButton.setText("UPDATE SALE PRICE");
			updateButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					addButton1.setVisible(true);
					jButton.setVisible(false);
					delButton1.setVisible(false);
					pricePanel.setVisible(true);
					for (int i=0;i<items.size();i++){
						if (!(items.get(i).getSaleFlag()))
							jComboBox.addItem(items.get(i).getName());
					}	
				}
			});
		}
		return updateButton;
	}

	/**
	 * This method initializes priceTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getPriceTextArea() {
		if (priceTextArea == null) {
			priceTextArea = new JTextArea();
			priceTextArea.setBounds(new Rectangle(50, 49, 56, 29));
		}
		return priceTextArea;
	}

	/**
	 * This method initializes priceTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPriceTextField() {
		if (priceTextField == null) {
			priceTextField = new JTextField();
			priceTextField.setBounds(new Rectangle(195, 51, 72, 31));
		}
		return priceTextField;
	}

	/**
	 * This method initializes addButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getAddButton1() {
		if (addButton1 == null) {
			addButton1 = new JButton();
			addButton1.setBounds(new Rectangle(484, 150, 81, 48));
			addButton1.setText("UPDATE");
			addButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int newprice  = Integer.parseInt(priceTextField.getText());
					priceTextField.setText("");
					String name = (String)jComboBox.getSelectedItem();
					catalog cat1 = new catalog();
					Object b1 = cat1.itemDisply();
					items = (List<Item>)b1; 
					Item it = new Item();
					it.setSale(newprice);
					it.setName(name);
					catalog cat = new catalog(it);
					Object b = cat.UpdateSale();
					Item sale = (Item)b;
					if (sale.getSaleFlag())
						okPanel.setVisible(true);
						jComboBox.removeAllItems();
						for (int i=0;i<items.size();i++){
							if (items.get(i).getName().equals(name)){
								items.get(i).setSaleFlag(sale.getSaleFlag());
								items.get(i).setSale(sale.getSale());
								newSaleArea.setText(Integer.toString(sale.getSale()));
							}
							if (items.get(i).getSaleFlag())
								jComboBox.addItem(items.get(i).getName());
							}
					}
			});
		}
		return addButton1;
	}

	/**
	 * This method initializes okPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getOkPanel() {
		if (okPanel == null) {
			textLabel = new JLabel();
			textLabel.setBounds(new Rectangle(17, 12, 121, 33));
			textLabel.setText("SALE ADDED");
			okPanel = new JPanel();
			okPanel.setLayout(null);
			okPanel.setBounds(new Rectangle(530, 29, 154, 103));
			okPanel.setBackground(Color.GREEN);
			okPanel.add(getOkButton(), null);
			okPanel.add(textLabel, null);
			okPanel.setVisible(false);
		}
		return okPanel;
	}

	/**
	 * This method initializes okButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setBounds(new Rectangle(34, 61, 74, 30));
			okButton.setText("OK");
			okButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					okPanel.setVisible(false);
				}
			});
		}
		return okButton;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(597, 153, 83, 48));
			jButton.setText("ADD");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int newprice  = Integer.parseInt(priceTextField.getText());
					priceTextField.setText("");
					String name = (String)jComboBox.getSelectedItem();
					catalog cat1 = new catalog();
					Object b1 = cat1.itemDisply();
					items = (List<Item>)b1; 
					Item it = new Item();
					it.setSale(newprice);
						it.setName(name);
						catalog cat = new catalog(it);
						Object b = cat.AddSale();
						Item sale = (Item)b;
						if (sale.getSaleFlag())
							okPanel.setVisible(true);
							jComboBox.removeAllItems();
							for (int i=0;i<items.size();i++){
								if (items.get(i).getName().equals(name)){
									items.get(i).setSale(sale.getSale());
									items.get(i).setSaleFlag(sale.getSaleFlag());
									newSaleArea.setText(Integer.toString(sale.getSale()));
								}
								if (!(items.get(i).getSaleFlag()))
									jComboBox.addItem(items.get(i).getName());
							}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes backButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	JButton getBackButton() {
		if (backButton == null) {
			backButton = new JButton();
			backButton.setBounds(new Rectangle(735, 512, 122, 43));
			backButton.setText("BACK");
		}
		return backButton;
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setBounds(new Rectangle(261, 235, 10, 17));
		}
		return jTextArea;
	}

	/**
	 * This method initializes newSaleArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getNewSaleArea() {
		if (newSaleArea == null) {
			newSaleArea = new JTextArea();
			newSaleArea.setBounds(new Rectangle(199, 89, 58, 30));
		}
		return newSaleArea;
	}

	/**
	 * This method initializes delButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getDelButton() {
		if (delButton == null) {
			delButton = new JButton();
			delButton.setBounds(new Rectangle(421, 78, 143, 63));
			delButton.setText("REMOVE SALE");
			delButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					addButton1.setVisible(false);
					jButton.setVisible(false);
					pricePanel.setVisible(false);
					for (int i=0;i<items.size();i++){
						if (items.get(i).getSaleFlag())
							jComboBox.addItem(items.get(i).getName());
					}
				}
			});
		}
		return delButton;
	}

	/**
	 * This method initializes pricePanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPricePanel() {
		if (pricePanel == null) {
			pricePanel = new JPanel();
			pricePanel.setLayout(null);
			pricePanel.setBounds(new Rectangle(57, 119, 312, 130));
			pricePanel.add(getPriceTextField(), null);
			pricePanel.add(getNewSaleArea(), null);
			pricePanel.add(newPrice, null);
			pricePanel.add(getPriceTextArea(), null);
			pricePanel.add(price, null);
			pricePanel.add(updateLabel, null);
			pricePanel.setVisible(false);
		}
		return pricePanel;
	}

	/**
	 * This method initializes delButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getDelButton1() {
		if (delButton1 == null) {
			delButton1 = new JButton();
			delButton1.setBounds(new Rectangle(537, 220, 94, 37));
			delButton1.setText("DELETE");
			delButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String name = (String)jComboBox.getSelectedItem();
					catalog cat1 = new catalog();
					Object b1 = cat1.itemDisply();
					items = (List<Item>)b1; 
					catalog cat = new catalog(name);
					Object b = cat.DeleteSale();
					Boolean sale = (Boolean)b;
					if (!sale){
						okDelPanel.setVisible(true);
						jComboBox.removeAllItems();
						for (int i=0;i<items.size();i++){
							if (items.get(i).getName().equals(name))	
								items.get(i).setSaleFlag(sale);
							if (items.get(i).getSaleFlag())
								jComboBox.addItem(items.get(i).getName());
						}
					}
				}
			});
		}
		return delButton1;
	}

	/**
	 * This method initializes okDelPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getOkDelPanel() {
		if (okDelPanel == null) {
			delLabel = new JLabel();
			delLabel.setBounds(new Rectangle(32, 10, 100, 43));
			delLabel.setText("SALE REMOVED");
			okDelPanel = new JPanel();
			okDelPanel.setLayout(null);
			okDelPanel.setBounds(new Rectangle(25, 26, 149, 113));
			okDelPanel.add(delLabel, null);
			okDelPanel.add(getOkDelButton(), null);
			okDelPanel.setVisible(false);
		}
		return okDelPanel;
	}

	/**
	 * This method initializes okDelButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOkDelButton() {
		if (okDelButton == null) {
			okDelButton = new JButton();
			okDelButton.setBounds(new Rectangle(40, 66, 72, 33));
			okDelButton.setText("OK");
			okDelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					okDelPanel.setVisible(false);
				}
			});
		}
		return okDelButton;
	}

	/**
	 * This method initializes errorPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getErrorPanel() {
		if (errorPanel == null) {
			errorLabel = new JLabel();
			errorLabel.setBounds(new Rectangle(17, 17, 122, 28));
			errorLabel.setText("INPUT WORNG");
			errorPanel = new JPanel();
			errorPanel.setLayout(null);
			errorPanel.setBounds(new Rectangle(290, 165, 161, 103));
			errorPanel.setBackground(Color.green);
			errorPanel.setVisible(false);
			errorPanel.add(getOkButton1(), null);
			errorPanel.add(errorLabel, null);
		}
		return errorPanel;
	}

	/**
	 * This method initializes okButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOkButton1() {
		if (okButton1 == null) {
			okButton1 = new JButton();
			okButton1.setBounds(new Rectangle(52, 60, 72, 33));
			okButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					errorPanel.setVisible(false);
				}
			});
		}
		return okButton1;
	}

}